package edu.gatech.oad.yuno;

import java.util.Date;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Spinner;
import android.widget.TabHost;

public class FilterActivity extends TabActivity implements OnRatingBarChangeListener, OnItemClickListener {
	private RatingBar priorityBar;
	private ListView categoryList;
	private ListView locationList;
	private EditText startDate;
	private EditText endDate;
	
	private int mStartYear;
	private int mStartMonth;
	private int mStartDay;
	private int mEndYear;
	private int mEndMonth;
	private int mEndDay;
	
	static final int START_DATE_DIALOG_ID = 0;
	static final int END_DATE_DIALOG_ID = 1;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter_dialog);
		
		Resources res = getResources();
	    TabHost tabHost = getTabHost();
	    TabHost.TabSpec spec;

	    spec = tabHost.newTabSpec("priority")
	    		.setIndicator("Priority", 
    				res.getDrawable(android.R.drawable.star_big_off))
				.setContent(R.id.tab1);
	    tabHost.addTab(spec);
	    
	    spec = tabHost.newTabSpec("location")
	    		.setIndicator("Location", 
    				res.getDrawable(android.R.drawable.ic_menu_compass))
				.setContent(R.id.tab2);
	    tabHost.addTab(spec);
	    
	    spec = tabHost.newTabSpec("category")
	    		.setIndicator("Category", 
    				res.getDrawable(android.R.drawable.ic_menu_sort_by_size))
				.setContent(R.id.tab3);
	    tabHost.addTab(spec);
	    
	    spec = tabHost.newTabSpec("date")
	    		.setIndicator("Date", 
    				res.getDrawable(android.R.drawable.ic_menu_today))
				.setContent(R.id.tab4);
	    tabHost.addTab(spec);
	    
	    tabHost.setCurrentTab(0);
	    
	    SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
	    priorityBar = (RatingBar) findViewById(R.id.filterPriority);
	    categoryList = (ListView) findViewById(R.id.categoryList);
		List<Category> categories = Category.findAllByUser(prefs.getLong("logged_in_user_id", 0));
		ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(this,
				android.R.layout.simple_list_item_1, categories);
		categoryList.setAdapter(adapter);
		
		locationList = (ListView) findViewById(R.id.locationList);
		List<Location> locations = Location.findAllByUser(prefs.getLong("logged_in_user_id", 0));
		ArrayAdapter<Location> locationAdapter = new ArrayAdapter<Location>(this,
				android.R.layout.simple_list_item_1, locations);
		locationList.setAdapter(locationAdapter);
		
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt("filter_priority",0);
		editor.putLong("filter_location", 0);
		editor.putLong("filter_category",0);
		editor.putLong("filter_startdate",0);
		editor.putLong("filter_enddate",0);
		editor.commit();
		
		priorityBar.setOnRatingBarChangeListener(this);
		locationList.setOnItemClickListener(this);
		categoryList.setOnItemClickListener(this);
		
		startDate = (EditText) findViewById(R.id.startDate);
		endDate = (EditText) findViewById(R.id.endDate);
		
		startDate.setOnClickListener(new View.OnClickListener() {

			//@Override
			public void onClick(View v) {
				showDialog(START_DATE_DIALOG_ID);
			}
		});
		
		endDate.setOnClickListener(new View.OnClickListener() {

			//@Override
			public void onClick(View v) {
				showDialog(END_DATE_DIALOG_ID);
			}
		});
		
		final Calendar cal = Calendar.getInstance();
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt("filter_priority",(int)rating);
		editor.commit();
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent == locationList) {
			Location location=(Location) parent.getAdapter().getItem(position);
			SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putLong("filter_location",location.getId());
			editor.commit();
			finish();
		} else if (parent == categoryList) {
			Category category=(Category) parent.getAdapter().getItem(position);
			SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putLong("filter_category",category.getId());
			editor.commit();
			finish();
		}
		
	}
	
	private DatePickerDialog.OnDateSetListener mStartDateSetListener =
			new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int month, int day) {
			mStartYear = year;
			mStartMonth = month;
			mStartDay = day;
			updateStartDateDisplay();
		}
	};
	
	private DatePickerDialog.OnDateSetListener mEndDateSetListener =
			new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int month, int day) {
			mEndYear = year;
			mEndMonth = month;
			mEndDay = day;
			updateEndDateDisplay();
		}
	};
	
	public void updateStartDateDisplay() {
		startDate.setText(new StringBuilder().append(mStartMonth + 1).append("/")
				.append(mStartDay).append("/").append(mStartYear));
	}
	
	public void updateEndDateDisplay() {
		endDate.setText(new StringBuilder().append(mEndMonth + 1).append("/")
				.append(mEndDay).append("/").append(mEndYear));
	}
	
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case START_DATE_DIALOG_ID:
			return new DatePickerDialog(this, mStartDateSetListener, mStartYear, mStartMonth, mStartDay);
		case END_DATE_DIALOG_ID:
			return new DatePickerDialog(this, mEndDateSetListener, mEndYear, mEndMonth, mEndDay);
		}
		return null;
	}
	
	@Override
	public void onPause() {
		Date dateStart = null;
		if (!startDate.getText().toString().equals(""))
			dateStart = new Date(Date.parse(startDate.getText().toString()));
		Date dateEnd = null;
		if (!endDate.getText().toString().equals(""))
			dateEnd = new Date(Date.parse(endDate.getText().toString()));
		
		SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		if (dateStart != null)
			editor.putLong("filter_startdate", dateStart.getTime());
		if (dateEnd != null)
			editor.putLong("filter_enddate", dateEnd.getTime());
		editor.commit();
		super.onPause();
	}
}
